var BrowserTab, BrowserTabs;
!function () {
    "use strict";
    var s = s || chrome, e = function () {
        yiclearCommon.BaseEvent.apply(this, arguments)
    };
    e.prototype = {__proto__: yiclearCommon.BaseEvent.prototype};
    var t = function () {
        e.call(this, s.tabs.onCreated)
    };
    t.prototype = {
        __proto__: e.prototype, specifyListener: function (t) {
            return function (e) {
                t(new BrowserTab(e))
            }
        }
    };
    var n = function () {
        e.call(this, s.tabs.onUpdated)
    };
    n.prototype = {
        __proto__: e.prototype, specifyListener: function (o) {
            return function (e, t, n) {
                "loading" == t.status && o(new BrowserTab(n))
            }
        }
    };
    var o = function () {
        e.call(this, s.tabs.onUpdated)
    };
    o.prototype = {
        __proto__: e.prototype, specifyListener: function (o) {
            return function (e, t, n) {
                "complete" == t.status && o(new BrowserTab(n))
            }
        }
    };
    var r = function () {
        e.call(this, s.tabs.onUpdated)
    };
    r.prototype = {
        __proto__: e.prototype, specifyListener: function (o) {
            return function (e, t, n) {
                o(new BrowserTab(n))
            }
        }
    };
    var i = function () {
        e.call(this, s.tabs.onActivated)
    };
    i.prototype = {
        __proto__: e.prototype, specifyListener: function (t) {
            return function (e) {
                s.tabs.get(e.tabId, function (e) {
                    c() || e && t(new BrowserTab(e))
                })
            }
        }
    };
    var a = function () {
        e.call(this, s.tabs.onRemoved)
    };

    function d(e) {
        var t = new BrowserTab({id: e.tabId});
        "websocket" === e.type && 0 === e.url.indexOf("http") && (e.url = e.url.replace(/^http(s)?:/, "ws$1:"));
        var n, o = {requestTime: e.timeStamp, requestUrl: e.url, tab: t}, r = 0, i = 0;
        switch (e.type) {
            case"main_frame":
                r = 0, n = p.RequestTypes.DOCUMENT;
                break;
            case"sub_frame":
                r = e.frameId, i = e.parentFrameId, n = p.RequestTypes.SUBDOCUMENT;
                break;
            default:
                i = e.frameId, n = e.type.toUpperCase()
        }
        return -1 == i && (i = 0), n === p.RequestTypes.OTHER && (n = function (e) {
            u.href = e;
            var t = u.pathname, n = p.platformUtils.parseContentTypeFromUrlPath(t);
            null == n && (n = p.RequestTypes.OBJECT);
            return n
        }(e.url)), o.frameId = r, o.requestFrameId = i, o.requestType = n, e.requestHeaders && (o.requestHeaders = e.requestHeaders), e.responseHeaders && (o.responseHeaders = e.responseHeaders), o
    }

    a.prototype = {
        __proto__: e.prototype, specifyListener: function (t) {
            return function (e) {
                t(new BrowserTab({id: e}))
            }
        }
    };
    var u = document.createElement("a");
    var c = function () {
        return s.runtime.lastError
    };
    (BrowserTab = function (e) {
        this.id = e.id, this.url = e.url, this.active = e.active, this.title = e.title, this.windowId = e.windowId, this.incognito = e.incognito
    }).prototype = {
        close: function () {
            s.tabs.remove(this.id, c)
        }, activate: function () {
            s.tabs.update(this.id, {selected: !0}, c), s.windows.update(this.windowId, {focused: !0}, c)
        }, sendMessage: function (e, t) {
            (s.tabs.sendMessage || s.tabs.sendRequest)(this.id, e, t)
        }, reload: function (e) {
            e ? s.tabs.update(this.id, {url: e}, c) : s.tabs.reload(this.id, {}, c)
        }, executeScript: function (e, t) {
            s.tabs.executeScript(this.id, e, function () {
                c() || t()
            })
        }, insertCSS: function (e, t) {
            s.tabs.insertCSS(this.id, e, function () {
                c() || t()
            })
        }, equals: function (e) {
            return this.id == e.id
        }
    }, (BrowserTabs = function (e) {
        this.tabsById = {}, this.incognitoTabsById = {}, this.options = e, this._delete = this._delete.bind(this)
    }).prototype = {
        get: function (e) {
            return (this.tabsById[e.id] || {}).value
        }, set: function (e, t) {
            this.tabsById[e.id] = {tab: e, value: t}
        }, has: function (e) {
            return e.id in this.tabsById
        }, clear: function () {
            for (var e in this.tabsById) this._delete(this.tabsById[e].tab)
        }, collection: function () {
            var e = [];
            for (var t in this.tabsById) e.push(this.tabsById[t].value);
            return e
        }, checkIncognitoMode: function (e) {
            this.incognitoTabsById[e.id] = e.incognito
        }, isIncognito: function (e) {
            return this.incognitoTabsById[e.id]
        }, remove: function (e) {
            this._delete(e)
        }, _delete: function (e) {
            delete this.tabsById[e.id], delete this.incognitoTabsById[e.id]
        }
    };
    var f = function (e) {
        this.id = e.id, this.visible = "minimized" != e.state
    };
    f.prototype = {
        getAllTabs: function (t) {
            s.tabs.query({windowId: this.id}, function (e) {
                t(e.map(function (e) {
                    return new BrowserTab(e)
                }))
            })
        }, getActiveTab: function (t) {
            var e = {windowId: this.id, active: !0};
            s.tabs.query(e, function (e) {
                e && 0 < e.length && t(new BrowserTab(e[0]))
            })
        }, openTab: function (e, t, n) {
            var o = {windowId: this.id, url: e, active: !t};
            s.tabs.create(o, function (e) {
                n && n(new BrowserTab(e))
            })
        }
    };
    var p = window.vAPI || {};
    p.ext = {}, p.ext.ver = null, p.ext.getURL = s.extension.getURL, p.ext.onMessage = new yiclearCommon.OnMessageEvent, p.ext.i18n = s.i18n, p.ext.backgroundPage = {}, p.ext.backgroundPage.getWindow = function () {
        return s.extension.getBackgroundPage()
    }, p.ext.backgroundPage.sendMessage = yiclearCommon.SendMessageFunction, p.ext.app = {
        getId: function () {
            return s.runtime.id
        }, getVersion: function () {
            return s.runtime.getManifest().version
        }, getLocale: function () {
            return s.i18n.getUILanguage()
        }, getUrlScheme: function () {
            var e = p.ext.getURL("bg.html"), t = e.indexOf("://");
            return e.substring(0, t)
        }, getBrowserVersion: function () {
            return null != p.ext.ver || (p.ext.ver = parseInt(/\d+/i.exec(/Chrome\/\d+\.\d+\.\d+\.\d+/i.exec(navigator.userAgent)))), p.ext.ver
        }
    }, p.ext.windows = {
        getAll: function (t) {
            s.windows.getAll(function (e) {
                t(e.map(function (e) {
                    return new f(e)
                }))
            })
        }, getLastFocused: function (t) {
            s.windows.getLastFocused(function (e) {
                t(new f(e))
            })
        }, getOrCreate: function (o, t) {
            var r = function (e) {
                return !("normal" != e.type || e.incognito && t)
            };
            s.windows.getLastFocused(function (e) {
                r(e) ? o(new f(e)) : s.windows.getAll(function (e) {
                    for (var t = 0; t < e.length; t++) {
                        var n = e[t];
                        if (r(n)) return void function (e) {
                            s.windows.update(e.id, {focused: !0}, function () {
                                c() || o(new f(e))
                            })
                        }(n)
                    }
                    s.windows.create({}, function (e) {
                        o(new f(e))
                    })
                })
            })
        }, onFocusChanged: {
            addListener: function (t) {
                s.windows.onFocusChanged.addListener(function (e) {
                    e != s.windows.WINDOW_ID_NONE && t()
                })
            }
        }, createPopup: function (e, t) {
            s.windows.create({url: e, type: "popup", width: 1230, height: 630}, function (e) {
                t && t(new f(e))
            })
        }
    }, p.ext.tabs = {
        onCreated: new t,
        onLoading: new n,
        onUpdated: new r,
        onCompleted: new o,
        onActivated: new i,
        onRemoved: new a,
        sendMessage: s.tabs.sendMessage,
        remove: s.tabs.remove,
        getLastFocused: function (t) {
            s.tabs.query({lastFocusedWindow: !0, active: !0}, function (e) {
                t && e && 0 < e.length && t(new BrowserTab(e[0]))
            })
        }
    };
    var b = {
        addListener: function (o, e) {
            s.webRequest.onBeforeRequest.addListener(function (e) {
                if (-1 !== e.tabId) {
                    var t = d(e), n = o(t);
                    return 1 != n && 0 != n ? n : {cancel: 0 == n}
                }
            }, e ? {urls: e} : {}, ["blocking"])
        }
    }, w = {
        addListener: function (o, e) {
            s.webRequest.onHeadersReceived.addListener(function (e) {
                if (-1 !== e.tabId) {
                    var t = d(e), n = o(t);
                    return n ? "responseHeaders" in n ? {responseHeaders: n.responseHeaders} : {} : void 0
                }
            }, e ? {urls: e} : {}, ["responseHeaders", "blocking"])
        }
    }, l = {
        addListener: function (o, e) {
            var t = ["blocking", "requestHeaders"];
            s.webRequest.OnBeforeSendHeadersOptions.hasOwnProperty("EXTRA_HEADERS") && t.push("extraHeaders"), s.webRequest.onBeforeSendHeaders.addListener(function (e) {
                if (-1 !== e.tabId) {
                    var t = d(e), n = o(t);
                    return n ? "requestHeaders" in n ? {requestHeaders: n.requestHeaders} : {} : void 0
                }
            }, e ? {urls: e} : {}, t)
        }
    };
    p.ext.webRequest = {
        onBeforeRequest: b,
        handlerBehaviorChanged: s.webRequest.handlerBehaviorChanged,
        onCompleted: s.webRequest.onCompleted,
        onErrorOccurred: s.webRequest.onErrorOccurred,
        onHeadersReceived: w,
        onBeforeSendHeaders: l,
        webSocketSupported: void 0 !== s.webRequest.ResourceType && "websocket" === s.webRequest.ResourceType.WEBSOCKET
    };
    var g = {
        addListener: function (t) {
            void 0 !== s.webNavigation.onCreatedNavigationTarget && s.webNavigation.onCreatedNavigationTarget.addListener(function (e) {
                -1 !== e.tabId && t({tabId: e.tabId, sourceTabId: e.sourceTabId, url: e.url})
            })
        }
    }, h = {
        addListener: function (t) {
            s.webNavigation.onCommitted.addListener(function (e) {
                -1 !== e.tabId && t(e.tabId, e.frameId, e.url)
            })
        }
    };
    p.ext.webNavigation = {
        onCreatedNavigationTarget: g,
        onCommitted: h
    }, p.ext.browserAction = {
        setBrowserAction: function (e, t, n, o, r) {
            var i = e.id;
            s.browserAction.setIcon({tabId: i, path: t}, function () {
                c() || s.tabs.get(i, function () {
                    c() || (s.browserAction.setBadgeText({
                        tabId: i,
                        text: n
                    }), c() || (n && (s.browserAction.setBadgeBackgroundColor({
                        tabId: i,
                        color: o
                    }), c()), s.browserAction.setTitle({tabId: i, title: r})))
                })
            })
        }
    }, p.ext.contextMenus = s.contextMenus
}();